//
//  CKPersonInfoViewController.m
//  ckjntiku
//
//  Created by Eileen Vivian on 2017/12/7.
//  Copyright © 2017年 Eileen Vivian. All rights reserved.
//

#import "CKPersonInfoViewController.h"
#import <AFNetworking.h>
@interface CKPersonInfoViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *saveBtn;
@property (weak, nonatomic) IBOutlet UIImageView *touxiangImage;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *idTF;
@property (weak, nonatomic) IBOutlet UITextField *otherNameTF;
@property (weak, nonatomic) IBOutlet UITextField *eareTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;


@end

@implementation CKPersonInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人资料";
    [_saveBtn.layer setCornerRadius:25];
    _saveBtn.layer.masksToBounds = YES;
    [_touxiangImage.layer setCornerRadius:27];
    _touxiangImage.layer.masksToBounds = YES;
    _touxiangImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)];
    [_touxiangImage addGestureRecognizer:singleTap];
    NSData * imageData = [XMGUserDefaults objectForKey:@"userIcon"];
     UIImage *image=[[UIImage alloc] initWithData:imageData];
    _touxiangImage.image = image;
    
}
//  方法：alterHeadPortrait
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
    /**
     *  弹出提示框
     */
    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//方式1
        //允许编辑，即放大裁剪
        PickerImage.allowsEditing = YES;
        //自代理
        PickerImage.delegate = self;
        //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //初始化UIImagePickerController
        UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
        PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;//方式1
        //允许编辑，即放大裁剪
        PickerImage.allowsEditing = YES;
        //自代理
        PickerImage.delegate = self;
        //页面跳转
        [self presentViewController:PickerImage animated:YES completion:nil];
    }]];
    //按钮：取消，类型：UIAlertActionStyleCancel
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    //把newPhono设置成头像
//    _touxiangImage.image = newPhoto;
    NSData *dataImage = UIImagePNGRepresentation(newPhoto);
    [XMGUserDefaults setObject:dataImage forKey:@"userIcon"];
    _touxiangImage.image = newPhoto;
    //关闭当前界面，即回到主界面去
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)saveClick:(UIButton *)sender {
  [[NSNotificationCenter defaultCenter]postNotificationName:@"传值" object: _userNameTF.text];
//    [self setData];
    [self uploadFiles2];
    [self.navigationController popViewControllerAnimated: YES];
}
-(void)setData{
    NSDictionary * dict = @{
                            @"sid":@"2017122090259",
                            @"sname":_userNameTF.text,
                            @"cid":_idTF.text,
                            @"sname2":_otherNameTF.text,
                            @"email":_codeTF.text,
                            @"add":_eareTF.text,
                            };
    [[AFHTTPSessionManager manager] POST:@"http://118.244.206.85:8060/Service.asmx/tkupdata" parameters:dict  progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(void)setImage{
     AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr POST:@"http://118.244.206.85:8060/Service.asmx/tkupdata"  parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/placeholder.png"] name:@"file" error:nil];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
  }
- (void)uploadFiles2{
    NSDictionary * dict = @{
                            @"sid":@"2017122090259",
                            @"sname":_userNameTF.text,
                            @"cid":_idTF.text,
                            @"sname2":_otherNameTF.text,
                            @"email":_codeTF.text,
                            @"add":_eareTF.text,
                            };
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    [manager  POST:@"http://118.244.206.85:8060/Service.asmx/saveimg" parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSData *fileData = UIImageJPEGRepresentation(self.touxiangImage.image, 1.0);
        [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
        //        NSString * path1 = [[NSBundle mainBundle] pathForResource:@"icon.png" ofType:nil];
        //        NSData * data1 = [NSData dataWithContentsOfFile:path1];
        //        [formData appendPartWithFileData:data1 name:@"user_img" fileName:@"2017122090259.png" mimeType:@"image/jpeg"];
        //        NSString * path2 = [[NSBundle mainBundle]pathForResource:@"image.png" ofType:nil];
        //        NSData * data2 = [NSData dataWithContentsOfFile:path2];
        //        [formData appendPartWithFileData:data2 name:@"userfile[]" fileName:@"67.jpg" mimeType:@"image/png"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
